package com.example.demo;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@RequestMapping("/class")
@RestController
public class Test {
    @RequestMapping("/r1")
    public String  haha(){
        return "hahha";
    }

    //传递json
    @RequestMapping("/r2")
    public String  r2(@RequestBody UserInfo userInfo){

        System.out.println(userInfo.getAge());
        return userInfo.toString();
    }

    //从url中获取参数  http://127.0.0.1:8080/class/chat/15   一个参数
    @RequestMapping("/chat/{chatId}")
    public String  r3(@PathVariable Integer chatId){
        return "获取id"+chatId;   //15
    }

    //从url中获取参数  http://127.0.0.1:8080/class/chat/block/44  两个参数
    @RequestMapping("/chat/{chat}/{number}")
    public String  r4(@PathVariable String chat,@PathVariable String number){
        return "获取类型"+chat+"获取id"+number;  //获取类型block获取id44
    }

    //上传文件
    @RequestMapping("/r3") //@RequestPart("file11")重命名,将file11赋值给file
    public String  r5(@RequestPart("file11") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        //文件上传
        file.transferTo(new File("C:\\Users\\123\\Pictures\\Camera Roll"+file.getOriginalFilename()));
        return "文件获取成功";
    }
}
